package com.remoteapp.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import android.app.Activity;
import android.os.Environment;

import com.remoteapp.activities.MainActivity;

public class AppConfig {

	/** The saved/loaded values of the configuration */
	public static String IP_ADDRESS = "";
	public static int PORT = -1;
	public static boolean AUTO_CONNECT = false;
	public static String[] GPIO_NAMES = { "GPIO_00", "GPIO_01", "GPIO_02",
			"GPIO_03", "GPIO_04", "GPIO_05", "GPIO_06", "GPIO_07" };
	public static boolean[] INVERT_GPIOS = { false, false, false, false, false,
			false, false, false };

	/** Configuration file name */
	private static String fileName = Environment.getExternalStorageDirectory()
			+ "/raspiConfig.properties";

	/** The text of all configuration item */
	private static String ITEM_IP_ADRESS = "IP";
	private static String ITEM_PORT = "Port";
	private static String ITEM_AUTO_CONNECT = "Auto Connect";
	private static String ITEM_GPIO_NAMES = "GPIO Names";
	private static String ITEM_INVERT_GPIOS = "Invert GPIOs";

	private static Activity main = null;

	/**
	 * Saves the configuration
	 * 
	 * @throws IOException
	 *             Is thrown when some error appears with saving the file
	 */
	public static void saveConfig() throws IOException {

		// Check configuration file
		File f = new File(fileName);
		if (!f.exists()) {
			f.createNewFile();
		}

		// Save configuration
		Properties cfg = new Properties();

		// Set all properties for configuration file
		cfg.setProperty(ITEM_IP_ADRESS, IP_ADDRESS);
		cfg.setProperty(ITEM_PORT, String.valueOf(PORT));
		cfg.setProperty(ITEM_AUTO_CONNECT, String.valueOf(AUTO_CONNECT));

		// Save GPIO names
		String nameString = "";
		String invString = "";
		for (int n = 0; n < GPIO_NAMES.length; n++) {
			nameString = nameString.concat(GPIO_NAMES[n] + ",");
			invString = invString.concat(String.valueOf(INVERT_GPIOS[n]) + ",");
		}

		cfg.setProperty(ITEM_GPIO_NAMES, nameString);
		cfg.setProperty(ITEM_INVERT_GPIOS, invString);

		// store configuration
		cfg.store(new FileOutputStream(f), null);
	}

	/**
	 * Tries to read configuration
	 * 
	 * @return TRUE = configuration load succeed / FALSE = configuration load
	 *         failed
	 * @throws IOException
	 *             Is thrown when file can not be found
	 */
	public static boolean loadConfig() throws IOException {

		// Check if file exists
		File f = new File(fileName);
		if (!f.exists()) {
			return false;
		}

		// Load configuration
		Properties cfg = new Properties();

		cfg.load(new FileInputStream(f));

		// Get all properties from configuration file
		IP_ADDRESS = cfg.getProperty(ITEM_IP_ADRESS);
		PORT = Integer.parseInt(cfg.getProperty(ITEM_PORT));
		AUTO_CONNECT = Boolean.parseBoolean(cfg.getProperty(ITEM_AUTO_CONNECT));

		// Load GPIO names
		String[] splNames = cfg.getProperty(ITEM_GPIO_NAMES).split(",");
		String[] splInvs = cfg.getProperty(ITEM_INVERT_GPIOS).split(",");
		for (int n = 0; n < splNames.length; n++) {

			if (!splNames[n].isEmpty()) {
				GPIO_NAMES[n] = splNames[n];
				INVERT_GPIOS[n] = Boolean.valueOf(splInvs[n]);
			}
		}

		return true;
	}

	/**
	 * Sets the main activity
	 * 
	 * @param act
	 *            The activity
	 */
	public static void setMainActivity(Activity act) {
		main = (MainActivity) act;
	}

	/**
	 * @return The main activity
	 */
	public static Activity getMainActivity() {
		return main;
	}

}
